<template>
	<view class="withdrawal-qrcode" :style="{'--height':`${topNavbarBgHeight}rpx`}">
		<common-navbar title="我的收款码" :background="background" backIconColor="#ffffff" titleColor="#ffffff" />
		<view class="user-avatar-wrapper">
			<img class="user-avatar" :src="userInfo.avatars"></img>
		</view>
		<view class="title-wrapper">
			我的个人收款二维码
		</view>
		<view class="withdrawal-qrcode-wrapper">
			<view class="qrcode-wrapper">
				<u-image :src="qrCode.file_url" width="100%" height="100%" v-if="qrCode" mode="aspectFill"></u-image>
			</view>
		</view>
		<button class="upload-btn" @click="upload">
			{{qrCode?'更换收款二维码':'上传收款二维码'}}
		</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				background: {
					backgroundColor: '#188cf1'
				},
				qrCode: null,
			}
		},
		async onLoad() {
			await this.$loginStatus;
			this.getQRCode();
		},
		computed: {
			topNavbarBgHeight() {
				return 750 * (this.$systemInfo.statusBarHeight + 44) / this.$systemInfo.screenWidth;
			}
		},
		methods: {
			async getQRCode(){
				const {
					data
				} = await this.$u.post(this.$restUrl.getWithdrawalQRCode);
				console.log('data>>>>>>>>>>>>>>>>>>>>>>>>>>>>',data);
				this.qrCode = data;
			},
			upload() {
				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['album', 'camera'],
					maxDuration: 30,
					camera: 'back',
					success:(result)=> {
						console.log(result.tempFiles[0].tempFilePath)
						uni.uploadFile({
							url: `https://www.gdlzjt.com/api/upload/uploadImagesCos`,
							filePath:result.tempFiles[0].tempFilePath,
							name: 'file',
							success: async (res) => {
								console.log('res>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>',res);
								const data = JSON.parse(res.data);
								console.log(data.data.file_id)
								if (data.status_code == 200) {
									console.log('111111111111111111111111111111111111111111')
									await this.$u.post(this.$restUrl.uploadWithdrawalQRCode,{
										img_id:data.data.file_id
									})
									this.getQRCode();
									this.$showToast('上传二维码成功');
								} else {
									this.$showToast('上传二维码失败')
								}
							},
							fail: err => {
								console.log(err)
								this.$showToast('上传二维码失败')
							}
						})
					}
				})

			}
		}
	}
</script>

<style scoped lang="scss">
	@import "./withdrawalQRCode.scss";
</style>
